package com.dotstone.chipism.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.BarChartEntity;
import com.dotstone.chipism.bean.EventBusEntity;
import com.dotstone.chipism.bean.MonthItemEntity;
import com.dotstone.chipism.bean.WeekItemEntity;
import com.github.mikephil.chart_3_0_1v.animation.Easing;
import com.github.mikephil.chart_3_0_1v.charts.BarChart;
import com.github.mikephil.chart_3_0_1v.components.AxisBase;
import com.github.mikephil.chart_3_0_1v.components.YAxis;
import com.github.mikephil.chart_3_0_1v.data.BarEntry;
import com.github.mikephil.chart_3_0_1v.data.Entry;
import com.github.mikephil.chart_3_0_1v.formatter.IAxisValueFormatter;
import com.github.mikephil.chart_3_0_1v.formatter.IValueFormatter;
import com.github.mikephil.chart_3_0_1v.highlight.Highlight;
import com.github.mikephil.chart_3_0_1v.listener.OnChartValueSelectedListener;
import com.github.mikephil.chart_3_0_1v.utils.ViewPortHandler;

import org.greenrobot.eventbus.EventBus;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


public class WeekFragment extends Fragment {
    private View view;
    private BarChart mBarChart, xBarChart;
    private DecimalFormat mFormat;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       view=inflater.inflate(R.layout.fragment_week, container, false);
        mFormat = new DecimalFormat("#,###.##");
        final WeekItemEntity entity = new WeekItemEntity();
        xBarChart = (BarChart) view.findViewById(R.id.week_chart);
        List<BarEntry>[]entries = new ArrayList[1];
        //x轴坐标 控制显示位置，并不会在X轴上显示具体的值
        final float[] xlabels = new float[entity.getMonthDatas().size()];
        int[] color ={Color.parseColor("#54CBEA")};
        final String unit = "";
        //x轴坐标增量
        float delta = 1;

        for (int index = 0, len = entity.getMonthDatas().size(); index < len; index ++) {
            xlabels[index] = index * delta + delta;
        }
        entries[0] = entity.getMonthEntries();
        BarChartEntity barChartEntity = new BarChartEntity(xBarChart, entries, null, color, Color.parseColor("#999999"), 10f);
        /*属性修改设置*/
        barChartEntity.setBarWidth(0.55f);
        barChartEntity.setDrawValueAboveBar(true);
        xBarChart.setPinchZoom(false);
        xBarChart.setScaleEnabled(false);
        xBarChart.getLegend().setEnabled(false);
        xBarChart.getXAxis().setTextSize(9f);
        // xBarChart.getAxisLeft().setEnabled(false); // 隐藏左边 的坐标轴
        xBarChart.getAxisLeft().setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        xBarChart.animateY(500, Easing.EasingOption.EaseInOutQuart);
        /*X,Y坐标显示设置*/
        barChartEntity.setAxisFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                for (int index = 0,len = xlabels.length; index < len; index ++) {
                    if (value == xlabels[index]) {
                        return entity.getMonthDatas().get(index).getBeginTime() + "-" + entity.getMonthDatas().get(index).getEndTime();
                    }
                }
                return "";
            }
        }, new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return mFormat.format(value) + (unit.equals("%") ? unit : "");
            }
        });

        /*值显示设置*/
        xBarChart.getData().setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return mFormat.format(value) + (unit.equals("%") ? unit : "");
            }
        });
        xBarChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                float y1=e.getY();
                Log.e("y1=",""+y1);
                EventBus.getDefault().post(
                        new EventBusEntity("value",""+y1));
            }

            @Override
            public void onNothingSelected() {

            }
        });

        /*Y轴最大值最小值设置，为了尽可能的显示完整数据*/
        float yMax = xBarChart.getData().getYMax() == 0 ? 100f :xBarChart.getData().getYMax();
        float yDelta = yMax / 5.5f;
        float axisMaximum = yMax + yDelta;
        if (axisMaximum < 5) {
            axisMaximum = 5;
        }
        xBarChart.getAxisLeft().setAxisMaximum(axisMaximum);
        float yMin = xBarChart.getData().getYMin();
        float deltaMin = yMin / 5.0f;
        xBarChart.getAxisLeft().setAxisMinimum(yMin - deltaMin);
        /**
         * 设置X轴文字顺时针旋转角度
         */
        xBarChart.getXAxis().setLabelRotationAngle(0);
        xBarChart.getData().setDrawValues(false);
        return view;
    }



}
